<?php

use think\migration\Migrator;
use think\migration\db\Column;

class VisitTrigger extends Migrator
{
    public function up()
    {

        $visit = env('DB_PREFIX').'visit';
        $player = env('DB_PREFIX').'player';
        $sql = <<<sql
CREATE TRIGGER `tri_visit_insert_after` AFTER INSERT ON `{$visit}`
FOR EACH ROW begin
    UPDATE {$player} SET page_view=page_view+1 WHERE id=new.player_id;
end
sql;
        $this->execute($sql);
        $sql = <<<sql
CREATE TRIGGER `tri_visit_delete_after` AFTER DELETE ON `{$visit}`
FOR EACH ROW begin
    UPDATE {$player} SET page_view=page_view-1 WHERE id=old.player_id;
end
sql;
        $this->execute($sql);
    }

    public function down()
    {
        $this->execute('DROP TRIGGER if exists tri_visit_delete_after');
        $this->execute('DROP TRIGGER if exists tri_visit_insert_after');

    }
}
